package com.epam.command.user;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.command.logic.Command;
import com.epam.entity.TouristVoucher;
import com.epam.util.ConfigurationManager;

public class RemoveFromCartCommand implements Command {

	
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		HttpSession session = request.getSession();
		ArrayList<TouristVoucher> cart = (ArrayList<TouristVoucher>) session.getAttribute("cart");
		int voucherId = Integer.parseInt(request.getParameter("voucher"));
		String page = ConfigurationManager.getConfig(ConfigurationManager.OPERATION_SUCCESSFUL);
		
		Iterator<TouristVoucher> iter = cart.iterator();
		
		while (iter.hasNext()) {
			TouristVoucher tv = (TouristVoucher) iter.next();
			if (tv.getId()==voucherId) {
				iter.remove();
			}
		}
		
		session.setAttribute("cart", cart);
		return page;
		
		
	}

}
